#include "botserver.h"

BotServer::BotServer(QObject *parent) :
    QTcpServer(parent)
{
    socketList = new QList<int>();
}

void BotServer::MsgUpdate(QString newMsg)
{
    qDebug() << "***************EMITTING MESSAGE UPDATE*********************";
    emit MsgUpdating(newMsg);
}

void BotServer::SocketListDelete(int oldSocket)
{
    qDebug() << "***************EMITTING SOCKET LIST UPDATE (DELETE)*********************";
    //Delete From List
    socketList->removeAt(socketList->indexOf(oldSocket));
    emit SocketListUpdate(*socketList);
}

void BotServer::SocketListAdd(int newSocket)
{
    qDebug() << "***************EMITTING SOCKET LIST UPDATE (ADD)*********************";
    //Add to List
    socketList->append(newSocket);
    emit SocketListUpdate(*socketList);
}

void BotServer::incomingConnection(int socketDescriptor)
{
    qDebug() << socketDescriptor << "Connecting...";
    BotThread *thread = new BotThread(socketDescriptor, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
    connect(thread, SIGNAL(MsgChange(QString)), this, SLOT(MsgUpdate(QString)));
    connect(thread, SIGNAL(DeleteSocket(int)), this, SLOT(SocketListDelete(int)));
    connect(thread, SIGNAL(AddSocket(int)), this, SLOT(SocketListAdd(int)));
}

